$(function(){
	var popup=$("#popup").val();
	var gotourl=$("#gotourl").val();
	$('.skin-minimal input').iCheck({
		checkboxClass: 'icheckbox-blue',
		radioClass: 'iradio-blue',
		increaseArea: '20%'
	});

	//tab选项卡
	$.Huitab("#tab-system .tabBar span","#tab-system .tabCon","current","click","0");
	

	//角色权限自动checkbox
	$(".permission-list dt input:checkbox").click(function(){
		$(this).closest("dl").find("dd input:checkbox").prop("checked",$(this).prop("checked"));
	});
	$(".permission-list2 dd input:checkbox").click(function(){
		var l =$(this).parent().parent().find("input:checked").length;
		var l2=$(this).parents(".permission-list").find(".permission-list2 dd").find("input:checked").length;
		if($(this).prop("checked")){
			$(this).closest("dl").find("dt input:checkbox").prop("checked",true);
			$(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked",true);
		}
		else{
			if(l==0){
				$(this).closest("dl").find("dt input:checkbox").prop("checked",false);
			}
			if(l2==0){
				$(this).parents(".permission-list").find("dt").first().find("input:checkbox").prop("checked",false);
			}
		}
		
	});

	// // //textarea，换行显示
	var v=$("#allowip").val() || '';
	v=v.replace(/[,，]/g, "\r\n");
	$("#allowip").val(v);

	var m=$("#disallowip").val() || '';
	m=m.replace(/[,，]/g, "\r\n");
	$("#disallowip").val(m);

});


/*==========================================
	参数解释：
	title	标题
	url		请求的url
	id		需要操作的数据id
	w		弹出层宽度（缺省调默认值）
	h		弹出层高度（缺省调默认值）

==========================================*/


/*弹窗打开*/
function admin_open(title,url,id,w,h){
	layer_show(title,url,w,h);
}


/*单个删除*/
function admin_del(url,id){
	layer.confirm('确认要删除吗？',function(index){
		//此处请求后台程序，下方是成功后的前台处理……	
		$.ajax({ 
			url:url,
			type:"POST",			
			data:{id:id},
			success:function(result){
				if(result=='y'){
					layer.msg('已删除!',{icon:1,time:1000});
					setTimeout(function(){
						self.location.href=self.document.location.href;
					},1000)
					
				}else{
					layer.msg(result,{icon:1,time:1000});
				}									
			} 
		});
	});
}


//全选
$("#controlAll").click(function(){
   var checklist = document.getElementsByName("ids");
   if(document.getElementById("controlAll").checked){
       for(var i=0;i<checklist.length;i++){
	       checklist[i].checked = true;
       } 
   }else{
       for(var j=0;j<checklist.length;j++){
	       checklist[j].checked = false;
       }
   }
})




/*批量操作*/
function todoall(title,url,field){
	layer.confirm('确认要'+title+'吗？',function(index){
		var ids='';
		var r=document.getElementsByName("ids"); 
		for(var i=0;i<r.length;i++){
			 if(r[i].checked){
				ids += r[i].value+"|";		
		   }
		}

		if(ids=='' || ids==null){
			layer.msg('请选择项目!', {icon: 3,time:1000});
			exit();
		}

		$.ajax({
			url:url,
			type:"POST",			
			data:{ids:ids,attrfield:field},
			success:function(result){	
				if(result=='y'){
					layer.msg('批量操作成功!', {icon: 6,time:1000});
					setTimeout(function(){
						window.location.reload();
					},1000)
				}else{
					layer.msg(result, {icon: 5,time:1000});
				}									
			} 
		});
		
	});
}

/*批量处理*/
function todo_all(title,url,w,h){
	var ids='';
	var r=document.getElementsByName("ids"); 
	for(var i=0;i<r.length;i++){
		 if(r[i].checked){
			ids += r[i].value+",";	
	   }
	}
	//ids=ids.Substring(0,ids.Length-1);
	ids=ids.substring(0,ids.length-1);
	if(ids=='' || ids==null){
		layer.msg('请选择项目!', {icon: 3,time:2000});
		exit();
	}else{
		url+='ids/'+ids;
	}
	layer_show(title,url,w,h);

}



/*信息停用*/
function admin_stop(obj,id,url){
	layer.confirm('确认要停用吗？',function(index){
		//此处请求后台程序，下方是成功后的前台处理……
		$.ajax({ 
			url:url,
			type:"POST",			
			data:{id:id,status:0},
			success:function(result){	
				if(result=='y'){
					$(obj).parents("tr").find(".td-manage").prepend('<a onClick="admin_start(this,\''+id+'\',\''+url+'\')" href="javascript:;" title="启用" style="text-decoration:none"><i class="Hui-iconfont">&#xe615;</i></a>');
					$(obj).parents("tr").find(".td-status").html('<span class="label radius">已禁用</span>');
					$(obj).remove();
					layer.msg('已停用!',{icon: 5,time:1000});
					setTimeout(function(){
						window.location.reload();
					},1000)
				}else{
					alert(result);
				}									
			} 
		});
	});
}

/*信息停用启用*/
function admin_start(obj,id,url){
	layer.confirm('确认要启用吗？',function(index){
		//此处请求后台程序，下方是成功后的前台处理……
		$.ajax({ 
			url:url,
			type:"POST",			
			data:{id:id,status:1},
			success:function(result){	
				if(result=='y'){
					$(obj).parents("tr").find(".td-manage").prepend('<a onClick="admin_stop(this,\''+id+'\',\''+url+'\')" href="javascript:;" title="停用" style="text-decoration:none"><i class="Hui-iconfont">&#xe631;</i></a>');
					$(obj).parents("tr").find(".td-status").html('<span class="label label-success radius">已启用</span>');
					$(obj).remove();
					layer.msg('已启用!', {icon: 6,time:1000});
					setTimeout(function(){
						window.location.reload();
					},1000)
				}else{
					alert(result);
				}									
			} 
		});
		
		
	});
}


//设置每页显示数量值，存入cookie
function getpage(){
  var getpage = $("#line").val();
  setCookie('line', getpage, 7);
  window.location.reload();
}


//设置cookie
function setCookie(name, value, days) {
	var argc = setCookie.arguments.length;
	var argv = setCookie.arguments;
	var secure = (argc > 5) ? argv[5] : false;
	var expire = new Date();
	if(days==null || days==0) days=1;
	expire.setTime(expire.getTime() + 3600000*24*days);
	document.cookie = name + "=" + escape(value) + ("; path=/") + ((secure == true) ? "; secure" : "") + ";expires="+expire.toGMTString();
}



//分页跳转到指定页面
//function gotopage(totalpage){
//	var page=parseInt(document.getElementById("page").value);
//	var r = /^[0-9]*[1-9][0-9]*$/;
//	if(!r.test(page)){
//		alert('请输入正整数');
//		exit;
//	}
//	totalpage=parseInt(totalpage);
//	if(page>totalpage){
//	    alert('输入超过最大页面');
//		exit;
//	}
//	page=page-1;
//	var search=document.getElementById("search").value;
//	var pageurl=window.location.pathname + '?page=' + page + search;
//	window.location.href= pageurl;
//}

//textarea 值传给隐藏域
$(document).on("change keyup onpaste click","#allowip",function(){	
	var txt =$("#allowip").val();
	var arr = txt.split('\n');
	var res='';
	$.each(arr, function(i,val){      
	  if(val=='' || val==null){
		var br='';
	  }else{
		var br=',';
	  }
      res+=val+br;	  
	});
	res=res.substring(0,res.length-1);
	$("#allowips").val(res);
});

$(document).on("change keyup onpaste click","#disallowip",function(){	
	var txt =$("#disallowip").val();
	var arr = txt.split('\n');
	var res='';
	$.each(arr, function(i,val){      
      if(val=='' || val==null){
		var br='';
	  }else{
		var br=',';
	  }
      res+=val+br;	  
	});
	res=res.substring(0,res.length-1);
	$("#disallowips").val(res);
});





